﻿using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using static MrPP.SerriedCabinet.CabinetGroup;

namespace MrPP.SerriedCabinet
{
    public class CellCheck : MonoBehaviour
    {
        [SerializeField]
        private TMPro.TextMeshProUGUI _all;
        [SerializeField]
        private TextMeshProUGUI _left;
        [SerializeField]
        private TextMeshProUGUI _goods;
        [SerializeField]
        private TextMeshProUGUI _check;


        [SerializeField]
        private Slider _slider;

        [SerializeField]
        private Transform _offset;

        internal void reset()
        {
            all =  new List<ItemData>();
            check_.Clear();
            left_.Clear();
            table_ = null;
        }

        [SerializeField]
        private Location _location;

        Dictionary<string, ItemData> table_ = null;
        private HashSet<string> check_ = new HashSet<string>();
        private List<ItemData> left_ = new List<ItemData>();


       
        private List<ItemData> all_ = new List<ItemData>();
        //所有
        public List<ItemData> all {
            private get {
                return all_;
            }
            set {
                all_ = value;
                table_ = new Dictionary<string, ItemData>();
                foreach (var item in all) {
                    table_.Add(item.sn, item);
                }
               
                refresh();
            }
        }

        internal void addChecked(ItemData data)
        {
            addChecked(data.sn);
        }
       
        string last_ = null;
        public void addChecked(string sn) {
            if (table_.ContainsKey(sn)) {
                last_ = table_[sn].text;
                table_.Remove(sn);
                check_.Add(sn);
            }
           
            refresh();
        }
        public void refresh() {
            if (all.Count != 0)
            {

                this.gameObject.SetActive(true);
            }
            else {

                this.gameObject.SetActive(false);
                return;
            }
            left_.Clear();
            foreach (var item in table_)
            {
                left_.Add(item.Value);
            }


            _all.text = all.Count.ToString();
            _left.text = table_.Count.ToString();

            _slider.value = (float)(all.Count - table_.Count) / (float)(all.Count);
            _check.text = this.last_;
            if (_location.location.type == Box.Type.Left)
            {

                _offset.transform.localEulerAngles = new Vector3(0, 0, 0);
            }
            else {

                _offset.transform.localEulerAngles = new Vector3(0, 180, 0);
            }
            
        }
        float time_ = 6.0f;
        int n_ = 0;
        private void Update()
        {
            time_ += Time.deltaTime;
            if (left_.Count != 0 && time_ > 5.0f) {
                
                _goods.text = left_[n_ % left_.Count].text;
                time_ = 0.0f;
                n_++;
            }
        }
        void OnDisable()
        {
           // all.Clear();
           // refresh();
        }
    }
};